<!doctype html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Ansible," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.1" />






<meta name="description" content="1. ansible特点&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;不需要安装客户端，通过sshd去通信 &amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;基于模块工作，模块可以由任何语言开发 &amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;不仅支持命令">
<meta name="keywords" content="Ansible">
<meta property="og:type" content="article">
<meta property="og:title" content="Ansible 安装与配置">
<meta property="og:url" content="https://hcldirgit.github.io/2017/09/03/Ansible/1. Ansible 安装与配置/index.html">
<meta property="og:site_name" content="失落的乐章">
<meta property="og:description" content="1. ansible特点&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;不需要安装客户端，通过sshd去通信 &amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;基于模块工作，模块可以由任何语言开发 &amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;不仅支持命令">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/01.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/02.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/03.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/04.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/05.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/06.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/07.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/08.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/09.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/10.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/11.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/12.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/13.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/14.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/15.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/16.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/17.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/18.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/19.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/20.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/21.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/22.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/23.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/24.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/25.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/26.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/27.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/28.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/29.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/30.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/31.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/32.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/33.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/34.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/35.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/36.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/37.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/38.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/39.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/40.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/41.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/42.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/43.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/44.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/45.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/46.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/47.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/48.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/49.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/50.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/51.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/52.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/53.png?raw=true">
<meta property="og:updated_time" content="2017-09-01T08:39:53.346Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Ansible 安装与配置">
<meta name="twitter:description" content="1. ansible特点&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;不需要安装客户端，通过sshd去通信 &amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;基于模块工作，模块可以由任何语言开发 &amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;不仅支持命令">
<meta name="twitter:image" content="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/01.png?raw=true">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://hcldirgit.github.io/2017/09/03/Ansible/1. Ansible 安装与配置/"/>





  <title>Ansible 安装与配置 | 失落的乐章</title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  




<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
          m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
  ga('create', '85*****1', 'auto');
  ga('send', 'pageview');
</script>


  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?87980c**************99ec5e26fb5";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>











  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">失落的乐章</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">技术面前，永远都是学生。</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-message">
          <a href="/message" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-external-link"></i> <br />
            
            留言
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="https://hcldirgit.github.io/2017/09/03/Ansible/1. Ansible 安装与配置/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="失落的乐章">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/0.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="失落的乐章">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Ansible 安装与配置</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-09-03T01:59:31+08:00">
                2017-09-03
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="1-ansible特点"><a href="#1-ansible特点" class="headerlink" title="1. ansible特点"></a>1. ansible特点</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;不需要安装客户端，通过sshd去通信</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;基于模块工作，模块可以由任何语言开发</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;不仅支持命令行使用模块，也支持编写yaml格式的playbook</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;支持sudo</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;有提供UI（浏览器图形化）www.ansible.com/tower  10台主机以内免费</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;开源UI  <a href="https://github.com/alaxli/ansible_ui" target="_blank" rel="external">https://github.com/alaxli/ansible_ui</a> </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;文档 <a href="http://download.csdn.net/detail/liyang23456/7741185" target="_blank" rel="external">http://download.csdn.net/detail/liyang23456/7741185</a></p>
<h2 id="2-ansible-安装"><a href="#2-ansible-安装" class="headerlink" title="2.ansible 安装"></a>2.ansible 安装</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;两台机器 192.168.0.82    192.168.0.81</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;只需要在192.168.0.82上安装ansible即可</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># yum install -y epel-release</span></div><div class="line">[root@ansible ~]<span class="comment"># yum install -y ansible</span></div></pre></td></tr></table></figure>
<h3 id="ansible-配置密钥"><a href="#ansible-配置密钥" class="headerlink" title="ansible 配置密钥"></a>ansible 配置密钥</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;192.168.0.82上生成密钥对</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;ssh-keygen -t rsa  直接回车即可，不用设置密钥密码</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># ssh-keygen -t rsa</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;把公钥(id_rsa.pub）内容放到对方机器（111）的/root/.ssh/authorized_keys里面</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># scp .ssh/id_rsa.pub 192.168.0.81:/root/.ssh/authorized_keys</span></div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/01.png?raw=true" alt="01"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;本机也要操作 </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># cat /root/.ssh/id_rsa.pub &gt;&gt; /root/.ssh/authorized_keys</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># chmod 600 /root/.ssh/authorized_keys</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;关闭selinux</p>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/02.png?raw=true" alt="02"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;检测连接</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># yum install -y openssh-clients</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># ssh 192.168.0.81</span></div><div class="line">Nasty PTR record <span class="string">"192.168.0.81"</span> is <span class="built_in">set</span> up <span class="keyword">for</span> 192.168.0.81, ignoring</div><div class="line">Last login: Thu Feb 23 22:34:20 2017 from 192.168.0.100</div><div class="line">[root@localhost ~]<span class="comment">#</span></div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/03.png?raw=true" alt="03"></p>
<h3 id="ansible-更改配置文件"><a href="#ansible-更改配置文件" class="headerlink" title="ansible 更改配置文件"></a>ansible 更改配置文件</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># vim /etc/ansible/hosts</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;增加</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[testhost]</div><div class="line">127.0.0.1</div><div class="line">192.168.0.81</div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/04.png?raw=true" alt="04"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明： testhost为主机组名字，自定义的。 下面两个ip为组内的机器ip。</p>
<h2 id="3-Ansible-远程执行命令"><a href="#3-Ansible-远程执行命令" class="headerlink" title="3.Ansible 远程执行命令"></a>3.Ansible 远程执行命令</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># ansible testhost -m command -a 'w'</span></div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/05.png?raw=true" alt="05"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这样就可以批量执行命令了。这里的testhost 为主机组名，-m后边是模块名字，-a后面是命令。当然我们也可以直接写一个ip，针对某一台机器来执行命令。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># ansible 127.0.0.1 -m command -a 'hostname'</span></div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/06.png?raw=true" alt="06"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;错误： “msg”: “Aborting, target uses selinux but python bindings (libselinux-python) aren’t installed!”</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;解决： </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># yum install -y libselinux-python</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;还有一个模块就是shell同样也可以实现</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># ansible testhost -m shell -a 'w'</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;shell 和 command 的区别</p>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/07.png?raw=true" alt="07"></p>
<h2 id="4-ansible-拷贝文件或者目录"><a href="#4-ansible-拷贝文件或者目录" class="headerlink" title="4.ansible 拷贝文件或者目录"></a>4.ansible 拷贝文件或者目录</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># ansible testhost -m copy -a "src=/etc/ansible dest=/tmp/ansibletest owner=root group=root mode=0644etest owner=root group=root mode=0755"</span></div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/08.png?raw=true" alt="08"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注意：源目录会放到目标目录下面去，如果目标指定的目录不存在，它会自动创建。如果拷贝的是文件，dest指定的名字和源如果不同，并且它不是已经存在的目录，相当于拷贝过去后又重命名。但相反，如果desc是目标机器上已经存在的目录，则会直接把文件拷贝到该目录下面。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># ansible testhost -m copy -a "src=/etc/passwd dest=/tmp/123"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这里的/tmp/123和源机器上的/etc/passwd是一致的，但如果目标机器上已经有/tmp/123目录，则会再/tmp/123目录下面建立passwd文件.如果没有123目录，则直接创建123文件。</p>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/09.png?raw=true" alt="09"></p>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/10.png?raw=true" alt="10"></p>
<h2 id="5-ansible-远程执行脚本"><a href="#5-ansible-远程执行脚本" class="headerlink" title="5.ansible 远程执行脚本"></a>5.ansible 远程执行脚本</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先创建一个shell脚本</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># vim /tmp/test.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加入内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">d=`date`</div><div class="line"><span class="built_in">echo</span> <span class="variable">$d</span> &gt; /tmp/an_test.txt</div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/11.png?raw=true" alt="11"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后把该脚本分发到各个机器上</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># ansible testhost -m copy -a "src=/tmp/test.sh dest=/tmp/test.sh mode=0755"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;最后是批量执行该shell脚本</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># ansible testhost -m shell -a "/tmp/test.sh"</span></div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/12.png?raw=true" alt="12"></p>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/13.png?raw=true" alt="13"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;shell模块，还支持远程执行命令并且带管道</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># ansible testhost -m shell -a "cat /etc/passwd | wc -l"</span></div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/14.png?raw=true" alt="14"></p>
<h2 id="6-ansible-实现任务计划"><a href="#6-ansible-实现任务计划" class="headerlink" title="6.ansible 实现任务计划"></a>6.ansible 实现任务计划</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># ansible testhost -m cron -a "name='test cron' job='/bin/touch /tmp/1212.txt' weekday=6"</span></div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/15.png?raw=true" alt="15"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;若要删除该cron 只需要加一个字段 state=absent</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># ansible testhost -m cron -a "name='test cron' state=absent"</span></div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/16.png?raw=true" alt="16"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;其他的时间表示：分钟 minute 小时 hour 日期 day 月份 month</p>
<h2 id="7-Ansible安装rpm包管理服务"><a href="#7-Ansible安装rpm包管理服务" class="headerlink" title="7.Ansible安装rpm包管理服务"></a>7.Ansible安装rpm包管理服务</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># ansible testhost -m yum -a "name=httpd"</span></div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/17.png?raw=true" alt="17"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在name后面还可以加上state=installed</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># ansible testhost -m service -a "name=httpd state=started enabled=yes"</span></div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/18.png?raw=true" alt="18"></p>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/19.png?raw=true" alt="19"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这里的name是centos系统里的服务名，可以通过chkconfig –list查到。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Ansible文档的使用</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;ansible-doc -l   列出所有的模块</p>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/20.png?raw=true" alt="20"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;ansible-doc cron  查看指定模块的文档</p>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/21.png?raw=true" alt="21"></p>
<h2 id="8-ansible-playbook-的使用"><a href="#8-ansible-playbook-的使用" class="headerlink" title="8.ansible playbook 的使用"></a>8.ansible playbook 的使用</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;相当于把模块写入到配置文件里面，例：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># vim /etc/ansible/test.yml</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;添加内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">---</div><div class="line">- hosts: testhost</div><div class="line">  remote_user: root</div><div class="line">  tasks:</div><div class="line">    - name: test_playbook</div><div class="line">    shell: touch /tmp/yanyi.txt</div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/22.png?raw=true" alt="22"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明： hosts参数指定了对哪些主机进行参作；</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;user参数指定了使用什么用户登录远程主机操作；</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;tasks指定了一个任务，其下面的name参数同样是对任务的描述，在执行过程中会打印出来。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># ansible-playbook /etc/ansible/test.yml</span></div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/23.png?raw=true" alt="23"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;192.168.0.82</p>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/24.png?raw=true" alt="24"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;192.168.0.81</p>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/25.png?raw=true" alt="25"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;再来一个创建用户的例子：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># vim /etc/ansible/create_user.yml</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;添加内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">---</div><div class="line">- name: create_user</div><div class="line">  hosts: testhost</div><div class="line">  user: root</div><div class="line">  gather_facts: <span class="literal">false</span></div><div class="line">  vars:</div><div class="line">    - user: <span class="string">"test"</span></div><div class="line">  tasks:</div><div class="line">    - name: create user</div><div class="line">    user: name=<span class="string">"&#123;&#123; user &#125;&#125;"</span></div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/26.png?raw=true" alt="26"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明： name参数对该playbook实现的功能做一个概述，后面执行过程中，会打印 name变量的值 ，可以省略；gather_facts参数指定了在以下任务部分执行前，是否先执行setup模块获取主机相关信息，这在后面的task会使用到setup获取的信息时用到；vars参数，指定了变量，这里指字一个user变量，其值为test ，需要注意的是，变量值一定要用引号引住；user提定了调用user模块，name是user模块里的一个参数，而增加的用户名字调用了上面user变量的值。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># ansible-playbook /etc/ansible/create_user.yml</span></div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/27.png?raw=true" alt="27"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;192.168.0.82</p>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/28.png?raw=true" alt="28"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;192.168.0.81</p>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/29.png?raw=true" alt="29"></p>
<h2 id="9-ansible-playbook-中的循环"><a href="#9-ansible-playbook-中的循环" class="headerlink" title="9.ansible playbook 中的循环"></a>9.ansible playbook 中的循环</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># vim /etc/ansible/loop.yml</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;添加内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">---</div><div class="line">- hosts: testhost</div><div class="line">  user: root</div><div class="line">  tasks:</div><div class="line">    - name: change mode <span class="keyword">for</span> files</div><div class="line">      file: path=/tmp/&#123;&#123; item &#125;&#125; mode=600</div><div class="line">      with_items:</div><div class="line">        - 1.txt</div><div class="line">        - 2.txt</div><div class="line">        - 3.txt</div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/30.png?raw=true" alt="30"></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># ansible-playbook /etc/ansible/loop.yml</span></div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/31.png?raw=true" alt="31"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;192.168.0.82 文件权限改为600</p>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/32.png?raw=true" alt="32"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;192.168.0.81 文件权限改为600</p>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/33.png?raw=true" alt="33"></p>
<h2 id="10-ansible-playbook-条件判断"><a href="#10-ansible-playbook-条件判断" class="headerlink" title="10.ansible playbook 条件判断"></a>10.ansible playbook 条件判断</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># vim /etc/ansible/when.yml</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;添加内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">---</div><div class="line">- hosts: testhost</div><div class="line">  user: root</div><div class="line">  gather_facts: True</div><div class="line">  tasks:</div><div class="line">    - name: use when</div><div class="line">      shell: touch /tmp/when.txt</div><div class="line">      when: ansible_eth0.ipv4.address == <span class="string">"192.168.0.82"</span></div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/34.png?raw=true" alt="34"></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># ansible-playbook /etc/ansible/when.yml</span></div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/35.png?raw=true" alt="35"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<a href="https://hcldirgit.github.io/2017/08/20/Ansible/2.%20ansible%E5%88%A4%E6%96%AD%20%E5%87%BA%E7%8E%B0FATAL%20all%20hosts%20have%20already%20failed%20--%20aborting%20%E9%94%99%E8%AF%AF/">ansible判断 出现FATAL all hosts have already failed – aborting 错误</a></p>
<h2 id="11-ansible-playbook-中的-handlers"><a href="#11-ansible-playbook-中的-handlers" class="headerlink" title="11.ansible playbook 中的 handlers"></a>11.ansible playbook 中的 handlers</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行task之后，服务器发生变化之后要执行的一些操作，比如我们修改了配置文件后，需要重启一下服务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># vim /etc/ansible/handlers.yml</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;添加内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">---</div><div class="line">- name: handlers <span class="built_in">test</span></div><div class="line">  hosts: 192.168.0.81</div><div class="line">  user: root</div><div class="line">  tasks:</div><div class="line">    - name: copy file</div><div class="line">    copy: src=/etc/passwd dest=/tmp/aaa.txt</div><div class="line">    notify: <span class="built_in">test</span> handlers</div><div class="line">  handlers:</div><div class="line">     - name: <span class="built_in">test</span> handlers</div><div class="line">       shell: <span class="built_in">echo</span> <span class="string">"111111"</span> &gt;&gt; /tmp/aaa.txt</div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/36.png?raw=true" alt="36"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明，只有copy模块真正执行后，才会去调用下面的handlers相关的操作。也就是说如果1.txt和2.txt内容是一样的，并不会去执行handlers里面的shell相关命令。 这种比较适合配置文件发生更改后，重启服务的操作。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># ansible-playbook /etc/ansible/handlers.yml</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;客户端查看</p>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/37.png?raw=true" alt="37"></p>
<h2 id="12-ansible-安装-nginx"><a href="#12-ansible-安装-nginx" class="headerlink" title="12.ansible 安装 nginx"></a>12.ansible 安装 nginx</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;思路：先在一台机器上编译安装好nginx、打包，然后再用ansible去下发</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;进入ansible配置文件目录</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># cd /etc/ansible</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;创建一个nginx_install的目录，方便管理</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ansible]<span class="comment"># mkdir nginx_install</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ansible]<span class="comment"># cd nginx_install/</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible nginx_install]<span class="comment"># mkdir -p roles/&#123;common,install&#125;/&#123;handlers,files,meta,tasks,templates,vars&#125;</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明：roles目录下有两个角色，common为一些准备操作，install为安装nginx的操作。每个角色下面又有几个目录，handlers下面是当发生改变时要执行的操作，通常用在配置文件发生改变，重启服务。files为安装时用到的一些文件，meta为说明信息，说明角色依赖等信息，tasks里面是核心的配置文件，templates通常存一些配置文件，启动脚本等模板文件，vars下为定义的变量</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;需要事先准备好安装用到的文件，具体如下：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在一台机器上事先编译安装好nginx，配置好启动脚本，配置好配置文件</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;安装好后，我们需要把nginx目录打包</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># cd /usr/local</span></div><div class="line">[root@ansible <span class="built_in">local</span>]<span class="comment"># tar zcvf nginx.tar.gz nginx</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;并放到/etc/ansible/nginx_install/roles/install/files/下面，名字为nginx.tar.gz</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible nginx_install]<span class="comment"># mv /usr/local/nginx.tar.gz /etc/ansible/nginx_install/roles/install/files/</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;启动脚本、配置文件都要放/etc/ansible/nginx_install/roles/install/templates下面</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@ansible roles]<span class="comment"># cd /etc/ansible/nginx_install/roles/install</span></div><div class="line">[root@ansible install]<span class="comment"># cp /usr/local/nginx/conf/nginx.conf templates/</span></div><div class="line">[root@ansible install]<span class="comment"># cp /etc/init.d/nginx templates/</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;定义common的tasks，nginx是需要一些依赖包的</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible nginx_install]<span class="comment"># cd /etc/ansible/nginx_install/roles</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible roles]<span class="comment"># vim ./common/tasks/main.yml</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;内容如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">---</div><div class="line">- name: Install initializtion require software</div><div class="line">  yum: name=&#123;&#123; item &#125;&#125; state=installed</div><div class="line">  with_items:</div><div class="line">    - zlib-devel</div><div class="line">    - pcre-devel</div><div class="line">    - openssl-devel</div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/38.png?raw=true" alt="38"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;定义变量</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible install]<span class="comment"># vim /etc/ansible/nginx_install/roles/install/vars/main.yml</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;内容如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">nginx_user: www</div><div class="line">nginx_port: 80</div><div class="line">nginx_basedir: /usr/<span class="built_in">local</span>/nginx</div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/39.png?raw=true" alt="39"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先要把所有用到的文档拷贝到目标机器</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible install]<span class="comment"># vim /etc/ansible/nginx_install/roles/install/tasks/copy.yml</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;内容如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">- name: Copy Nginx Software </div><div class="line">  copy: src=nginx.tar.gz dest=/tmp/nginx.tar.gz owner=root group=root</div><div class="line">- name: Uncompression Nginx Software</div><div class="line">  shell: tar zxf /tmp/nginx.tar.gz -C /usr/<span class="built_in">local</span>/</div><div class="line">- name: Copy Nginx Start Script</div><div class="line">  template: src=nginx dest=/etc/init.d/nginx owner=root group=root mode=0755</div><div class="line">- name: Copy Nginx Config</div><div class="line">  template: src=nginx.conf dest=&#123;&#123; nginx_basedir &#125;&#125;/conf/ owner=root group=root mode=0644</div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/40.png?raw=true" alt="40"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;接下来会建立用户，启动服务，删除压缩包</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># vim /etc/ansible/nginx_install/roles/install/tasks/install.yml</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;内容如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">- name: Create Nginx User</div><div class="line">  user: name=&#123;&#123; nginx_user &#125;&#125; state=present createhome=no shell=/sbin/nologin</div><div class="line">- name: Start Nginx Service</div><div class="line">  service: name=nginx state=restarted</div><div class="line">- name: Add Boot Start Nginx Service</div><div class="line">  shell: chkconfig --level 345 nginx on</div><div class="line">- name: Delete Nginx compression files</div><div class="line">  shell: rm -rf /tmp/nginx.tar.gz</div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/41.png?raw=true" alt="41"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;再创建main.yml并且把copy和install调用</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># vim /etc/ansible/nginx_install/roles/install/tasks/main.yml</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;内容如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">- include: copy.yml</div><div class="line">- include: install.yml</div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/42.png?raw=true" alt="42"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;到此两个roles：common和install就定义完成了，接下来要定义一个入口配置文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># vim /etc/ansible/nginx_install/install.yml</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;内容如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">---</div><div class="line">- hosts: testhost</div><div class="line">  remote_user: root</div><div class="line">  gather_facts: True</div><div class="line">  roles:</div><div class="line">    - common</div><div class="line">    - install</div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/43.png?raw=true" alt="43"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行： ansible-playbook /etc/ansible/nginx_install/install.yml</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># ansible-playbook /etc/ansible/nginx_install/install.yml</span></div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/44.png?raw=true" alt="44"></p>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/45.png?raw=true" alt="45"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>注意：执行之前一定要检查所有机器 80 端口是否占用，否则会出错！</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;客户端查看</p>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/46.png?raw=true" alt="46"></p>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/47.png?raw=true" alt="47"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下载整个样例库   </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;git clone git://github.com/dl528888/ansible-examples.git</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># git clone git://github.com/dl528888/ansible-examples.git</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;git命令，需要yum先安装一下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># yum install -y git</span></div></pre></td></tr></table></figure>
<h2 id="13-ansible-管理配置文件"><a href="#13-ansible-管理配置文件" class="headerlink" title="13.ansible 管理配置文件"></a>13.ansible 管理配置文件</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;生产环境中大多时候是需要管理配置文件的，安装软件包只是在初始化环境的时候用一下。下面我们来写个管理nginx配置文件的playbook</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># mkdir -p /etc/ansible/nginx_config/roles/&#123;new,old&#125;/&#123;files,handlers,vars,tasks&#125;</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;其中new为更新时用到的，old为回滚时用到的，files下面为nginx.conf和vhosts目录，handlers为重启nginx服务的命令<br>关于回滚，需要在执行playbook之前先备份一下旧的配置，所以对于老配置文件的管理一定要严格，千万不能随便去修改线上机器的配置，并且要保证new/files下面的配置和线上的配置一致</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;先把nginx.conf和vhosts目录放到files目录下面</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># cp /usr/local/nginx/conf/nginx.conf /etc/ansible/nginx_config/roles/new/files/</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># cp -r /usr/local/nginx/conf/vhosts /etc/ansible/nginx_config/roles/new/files/</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># vim /etc/ansible/nginx_config/roles/new/vars/main.yml</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;定义变量</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">nginx_basedir: /usr/<span class="built_in">local</span>/nginx</div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/48.png?raw=true" alt="48"></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># vim /etc/ansible/nginx_config/roles/new/handlers/main.yml</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;定义重新加载nginx服务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">- name: restart nginx</div><div class="line">  shell: /etc/init.d/nginx reload</div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/49.png?raw=true" alt="49"></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># vim /etc/ansible/nginx_config/roles/new/tasks/main.yml</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这是核心的任务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">- name: copy conf file</div><div class="line"> copy: src=&#123;&#123; item.src &#125;&#125; dest=&#123;&#123; nginx_basedir &#125;&#125;/&#123;&#123; item.dest &#125;&#125; backup=yes owner=root group=root mode=0644</div><div class="line"> with_items:</div><div class="line"> - &#123; src: nginx.conf, dest: conf/nginx.conf &#125;</div><div class="line"> - &#123; src: vhosts, dest: conf/ &#125;</div><div class="line"> notify: restart nginx</div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/50.png?raw=true" alt="50"></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># vim /etc/ansible/nginx_config//update.yml</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;最后是定义总入口配置</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">---</div><div class="line">- hosts: testhost</div><div class="line">  user: root</div><div class="line">  roles:</div><div class="line">    - new</div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/51.png?raw=true" alt="51"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行： </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># ansible-playbook /etc/ansible/nginx_config/update.yml</span></div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/52.png?raw=true" alt="52"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;而回滚的backup.yml对应的roles为old    </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># rsync -av /etc/ansible/nginx_config/roles/new/ /etc/ansible/nginx_config/roles/old/</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># ansible-playbook /etc/ansible/nginx_config/backup.yml</span></div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/53.png?raw=true" alt="53"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;回滚操作就是把旧的配置覆盖，然后重新加载nginx服务</p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Ansible/" rel="tag"># Ansible</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/09/03/Ansible/2. ansible判断 出现FATAL all hosts have already failed -- aborting 错误/" rel="next" title="ansible判断 出现FATAL all hosts have already failed -- aborting 错误">
                <i class="fa fa-chevron-left"></i> ansible判断 出现FATAL all hosts have already failed -- aborting 错误
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/09/03/Dns、Iredmaill/1. dns、iredmail邮件服务/" rel="prev" title="DNS、iredmaill 邮件服务">
                DNS、iredmaill 邮件服务 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/0.png"
               alt="失落的乐章" />
          <p class="site-author-name" itemprop="name">失落的乐章</p>
           
              <p class="site-description motion-element" itemprop="description">失落的乐章的Blog</p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/">
                <span class="site-state-item-count">627</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">38</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/hcldirgit" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-ansible特点"><span class="nav-number">1.</span> <span class="nav-text">1. ansible特点</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-ansible-安装"><span class="nav-number">2.</span> <span class="nav-text">2.ansible 安装</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#ansible-配置密钥"><span class="nav-number">2.1.</span> <span class="nav-text">ansible 配置密钥</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#ansible-更改配置文件"><span class="nav-number">2.2.</span> <span class="nav-text">ansible 更改配置文件</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-Ansible-远程执行命令"><span class="nav-number">3.</span> <span class="nav-text">3.Ansible 远程执行命令</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-ansible-拷贝文件或者目录"><span class="nav-number">4.</span> <span class="nav-text">4.ansible 拷贝文件或者目录</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-ansible-远程执行脚本"><span class="nav-number">5.</span> <span class="nav-text">5.ansible 远程执行脚本</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-ansible-实现任务计划"><span class="nav-number">6.</span> <span class="nav-text">6.ansible 实现任务计划</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#7-Ansible安装rpm包管理服务"><span class="nav-number">7.</span> <span class="nav-text">7.Ansible安装rpm包管理服务</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#8-ansible-playbook-的使用"><span class="nav-number">8.</span> <span class="nav-text">8.ansible playbook 的使用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#9-ansible-playbook-中的循环"><span class="nav-number">9.</span> <span class="nav-text">9.ansible playbook 中的循环</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#10-ansible-playbook-条件判断"><span class="nav-number">10.</span> <span class="nav-text">10.ansible playbook 条件判断</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#11-ansible-playbook-中的-handlers"><span class="nav-number">11.</span> <span class="nav-text">11.ansible playbook 中的 handlers</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#12-ansible-安装-nginx"><span class="nav-number">12.</span> <span class="nav-text">12.ansible 安装 nginx</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#13-ansible-管理配置文件"><span class="nav-number">13.</span> <span class="nav-text">13.ansible 管理配置文件</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">失落的乐章</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Muse
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.1"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.1"></script>



  


  




	





  





  





  






  





  

  

  

  

  

  

</body>
</html>
